/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * A form to create a new question
 */
qx.Class.define("bip.authorview.forms.QuestionPropertiesForm",
{
  extend : qx.ui.form.Form,
  
  include: [bip.authorview.forms.MControllerForm, qx.locale.MTranslation],
  
  construct : function()
  {
    this.base(arguments);
    // Add fields
    this.addGroupHeader("Question properties");
    // Name
    var nameTextfield = new qx.ui.form.TextField();
    nameTextfield.set({required:true,width:100,liveUpdate:true});
    this.add(nameTextfield, "Name", null, "name");
    // Description
    var descTextfield = new qx.ui.form.TextField();
    descTextfield.set({required:true,width:200,liveUpdate:true});
    this.add(descTextfield, "Description", null, "description");
    var intWidth=40;
    // Min
    var minValueField = new qx.ui.form.Spinner();
    minValueField.set({maximum: 100,minimum: -100,value:0,width:intWidth})
    this.add(minValueField, "MinValue", null, "minValue");
    // Max
    var maxValueField = new qx.ui.form.Spinner();
    maxValueField.set({maximum: 100,minimum: -100,value:1,width:intWidth});
    this.add(maxValueField, "MaxValue", null, "maxValue");
    // Default
    var defaultValueField = new qx.ui.form.Spinner();
    defaultValueField.set({maximum: 1,minimum: 0,value:0,width:intWidth});
    this.add(defaultValueField, "DefaultValue", null, "defaultValue");
    // TextValues
    this.__textValues = new bip.authorview.forms.QuestionTextValuesField();
    this.add(this.__textValues, "TextValues", null, "listTextValues");
    
    // Logic
    // Make sure max is always upper min
    minValueField.addListener("changeValue",function(evt) {
    	if (evt.getData() >= maxValueField.getValue()) {
    		maxValueField.setValue(evt.getData()+1);
    	}
    	if (evt.getData() > defaultValueField.getValue()) {
    		defaultValueField.setValue(evt.getData());
    	}
    	defaultValueField.setMinimum(evt.getData());
    },this);
    // Make sure min is always below max
    maxValueField.addListener("changeValue",function(evt) {
    	if (evt.getData() <= minValueField.getValue()) {
    		minValueField.setValue(evt.getData()-1);
    	}
    	if (evt.getData() < defaultValueField.getValue()) {
    		defaultValueField.setValue(evt.getData());
    	}
    	defaultValueField.setMaximum(evt.getData());
    },this);
    
    // Button
    var rmButton = bip.ui.form.PredefinedButtons.newDeleteButton();
    rmButton.addListener("execute",this.__onRemoveButtonClickHandler,this);
    this.addButton(rmButton);
    var saveButton = bip.ui.form.PredefinedButtons.newSaveButton("Update");
    saveButton.addListener("execute",this.__onSaveButtonClickHandler,this);
    this.addButton(saveButton);
    
  },
  
	/*
	 *****************************************************************************
    MEMBERS
	 *****************************************************************************
	 */

 	members :
 	{
 		
 		__onSaveButtonClickHandler: function(evt) {
			if (this.validate()) {
				this.getController().updateModel();
				bip.Application.setState("running",this.tr("Updating question, please wait"));
				bip.authorview.SharedServices.getInstance().getQuestionService().addListenerOnce("state",function(evt) {
					if (evt.getData().getState() == "ok") {
						bip.Application.setState(evt.getData().getState(),"The question has been updated");
						this.__textValues.setValue(evt.getData().getModel().getListTextValues());  // Update text values
					} else {
						bip.Application.setState(evt.getData().getState(),evt.getData().getModel());
					}
				},this);
				bip.authorview.SharedServices.getInstance().getQuestionService().postQuestion(this.getController().getModel());
			} else {
				bip.Application.setState("error","Please correct the error(s) on this form");
			}
		},

		
		__onRemoveButtonClickHandler: function(evt) {
			if (this.validate()) {
				this.getController().updateModel();
				bip.Application.setState("running",this.tr("Deleting question, please wait"));
				bip.authorview.SharedServices.getInstance().getQuestionService().addListenerOnce("state",function(evt) {
					if (evt.getData().getState() == "ok") {
						bip.Application.setState(evt.getData().getState(),"The question has been deleted");
						bip.services.Survey.getInstance().getSurveyTree();
					} else {
						bip.Application.setState(evt.getData().getState(),evt.getData().getModel());
					}
				});
				bip.authorview.SharedServices.getInstance().getQuestionService().removeQuestion(this.getController().getModel());
			} else {
				bip.Application.setState("error","Please correct the error(s) on this form");
			}
		}
		
		
 		
 	},
 	
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects();
	}

  
});